import express from "express"

// import history from "connect-history-api-fallback"
import userApi from './api/userApi'
import markerPoiApi from './api/markerPoiApi'
import journalApi from './api/journalApi'
import uploadApi from './api/uploadApi'
import emailApi from './api/emailApi'

import errorMiddleware from "./errorMiddleware"
import tokenMiddleware from "./tokenMiddleware"
import { PUBLIC_PATH } from "../config"
import helmet from 'helmet'

import ENV from "../env"
const { BASE_API, SERVICE_PORT } = ENV

const app = express()
// helmet ：这个软件包是一种中间件，可通过添加多个HTTP标头来帮助保护您的API。
app.use(helmet({
  contentSecurityPolicy: false,
}));

app.use(express.static(PUBLIC_PATH))

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(express.urlencoded({ extended: true }))
// 解析 application/json 格式的请求体
app.use(express.json())

app.use(tokenMiddleware)

app.use(`${BASE_API}/users`, userApi)
app.use(`${BASE_API}/markers`, markerPoiApi)
app.use(`${BASE_API}/journals`, journalApi)
app.use(`${BASE_API}/upload`, uploadApi)
app.use(`${BASE_API}/email`, emailApi)

app.use(errorMiddleware)

app.listen(SERVICE_PORT, () => {
  console.log('\x1B[36m%s\x1B[0m',
    `listen port ${SERVICE_PORT}, base api ${BASE_API}`
  )
})

